home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / serial / zyxel-1.5-s / zyxel-1 / hplay.c < prev    next >
C/C++ Source or Header  |  1995-01-03  |  2KB  |  83 lines

  1. #include <config.h>
  2. /*****************************************************************/
  3. /***                                                           ***/
  4. /***    Play out a file on stdout in u-law encoding            ***/
  5. /***                                                           ***/
  6. /***                H.F. Silverman 1/4/91                      ***/
  7. /***    Modified:   Rob Janssen (pe1chl@wab-tis.rabobank.nl)   ***/
  8. /***                adapted for use with ZyXEL                 ***/
  9. /***                                                           ***/
  10. /*****************************************************************/
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <math.h>
  15. #include <errno.h>
  16. #include <ctype.h>
  17. #include <unistd.h>
  18.  
  19. #include "proto.h"
  20. #include "getargs.h"
  21. #include "hplay.h"
  22. #include "l2u.h"
  23.  
  24. #define SAMP_RATE 9600
  25. long samp_rate = SAMP_RATE;
  26.  
  27. /* Audio Parameters */
  28.  
  29. char *prog = "hplay";
  30.  
  31. #ifdef DEBUG
  32. int min,max;
  33. #endif
  34.  
  35. int
  36. audio_init (int argc, char *argv[])
  37. {
  38.   prog = argv[0];
  39.   return argc;
  40. }
  41.  
  42. void
  43. audio_term ()
  44. {
  45.   close(1);
  46. #ifdef DEBUG
  47.   fprintf(stderr,"data range: %d..%d\n",min,max);
  48. #endif
  49. }
  50.  
  51. void
  52. audio_play (int n, short *data)
  53. {
  54.   if (n > 0)
  55.     {
  56.       unsigned char *converted = (unsigned char *) malloc (n);
  57.       int i;
  58.  
  59.       if (converted == NULL)
  60.     {
  61.       fprintf (stderr, "Could not allocate memory for conversion\n");
  62.       exit (3);
  63.     }
  64.  
  65.       for (i = 0; i < n; i++) {
  66. #ifdef DEBUG
  67.     if (*data > max)
  68.         max = *data;
  69.  
  70.     if (*data < min)
  71.         min = *data;
  72. #endif
  73.  
  74.     converted[i] = short2ulaw(2 * *data++);
  75.       }
  76.  
  77.       if (write (1, converted, n) != n)
  78.     perror ("write");
  79.  
  80.       free (converted);
  81.     }
  82. }
  83.